/**
 * Created by MD-3 on 2017/6/13.
 */
function DoorConstruct(doorId, doorMarker, doorState, alarmState){
    this.doorId = doorId;
    this.doorMarker = doorMarker;
    this.doorState = doorState;
    this.alarmState = alarmState;
}

var doorConstructList = [];
function addOverlay_Door() {
    var doorList = door_mock;
    var i=0;
    var door,doorId,doorName,doorState,alarmState,lng,lat;
    var doorMarker;

    for(i=0; i<doorList.length; i++) {
        door = doorList[i];
        doorId = door.doorId;
        doorName = door.doorName;
        doorState = door.doorState;
        alarmState = door.alarmState;
        lng = door.position.lng;
        lat = door.position.lat;

        doorMarker = addDoorMarker(lng, lat, doorName, doorState, alarmState);

        addMarkerListener(doorMarker, doorId);

        doorConstructList.push(new DoorConstruct(doorId,doorMarker,doorState,alarmState));
    }
}

function addDoorMarker(lng, lat, doorName, doorState, alarmState) {
    var icon = getDoorIcon(doorState, alarmState);
    var doorMarker = new AMap.Marker({
        map: map,
        icon: icon,
        position: [lng, lat],
        offset: new AMap.Pixel(-8, -8),
        label: {content: doorName, offset: new AMap.Pixel(15, 15)},
        topWhenClick: true
    });
    return doorMarker;
}

function addTaskPolyline(lineArr, lineColor, lineStyle) {
    var polyline = new AMap.Polyline({
        map: map,
        path: lineArr,          //设置线覆盖物路径
        strokeColor: lineColor, //线颜色
        strokeOpacity: 1,       //线透明度
        strokeWeight: 2,        //线宽，单位：像素
        strokeStyle: lineStyle,   //线样式,实线:solid，虚线:dashed
        strokeDasharray: [10, 5] //补充线样式
    });
}

function getDoorIcon(doorState,alarmState) {
    var icon = resource_mock.DOOR_CLOSE_CHECK;
    if(alarmState && "1"==alarmState){                                         //未处理
        switch(doorState) {
            case "0":
                icon = resource_mock.DOOR_CLOSE;
                break;
            case "1":
                icon = resource_mock.DOOR_OPEN;
                break;
            default:
                icon = resource_mock.DOOR_CLOSE_CHECK;
        }
    }else{
        switch(doorState) {
            case "0":
                icon = resource_mock.DOOR_CLOSE_CHECK;
                break;
            case "1":
                icon = resource_mock.DOOR_OPEN_CHECK;
                break;
            default:
                icon = resource_mock.DOOR_CLOSE_CHECK;
        }
    }
    return icon;
}

function addMarkerListener(doorMarker, doorId) {
    doorMarker.on('click', function () {
        openInfoWindow(doorId);                 //左击，信息窗口
    });
    doorMarker.on('rightclick', function () {
        openInfoWindow2(doorId);                 //右击，信息窗口
    });
}

function getDoorById(id) {
    if (!id || 0 > id) {
        return;
    }
    var door;
    $.each(doorConstructList, function (idx, doorConstruct) {
        if (id == doorConstruct.doorId) {
            door = doorConstruct;
            return true;
        }
    })
    return door;
}

